<template>
	<uni-nav-bar :border="false" backgroundColor="transparent" :fixed="true">
		<block v-slot:left>
			<uni-icons class="rr-icons" color="transparent" type="back" size="30"></uni-icons>
		</block>
	</uni-nav-bar>
	<view class="container">
		<view class="content">
			<view class="head">
				hello!!
				<br />
				<text>欢迎来到{{$store.state.name}}</text>
			</view>
			<view class="box">
				<view class="titlebox">
					<view class="left active">
						用户登录
					</view>
					<view class="right" @click="$goURl('/pages/login/register')">
						用户注册
					</view>
				</view>
				<view class="list">
					<view class="item">
						<view class="items">
							<view class="image">
								<image src="http://img.shibangszkj.top/shibang/16995013085934.png" mode=""></image>
							</view>
							<input class="uni-input" maxlength="20" v-model="form.mobile" placeholder="请输入手机号" />
						</view>
					</view>

					<view class="item">
						<view class="items">
							<view class="image">
								<image src="http://img.shibangszkj.top/shibang/16995013966448.png" mode=""></image>
							</view>
							<input class="uni-input" @confirm="login" v-model="form.password" placeholder="请输入密码"
								:password="showPassword" />
						</view>
						<uni-icons :type="!showPassword?'eye-filled':'eye-slash-filled'" size="20"
							@click="changePassword"></uni-icons>
					</view>

				</view>


				<view class="btnBox" @click="login" :style="{background:checked?'':'#ccc'}">
					登录
				</view>
				<view class="titleboxs">
					<!-- <view class="left">
						忘记密码
					</view> -->
					<view class="left" @click="$goURl('/pages/login/forget')">
						验证码登录
					</view>
					<view class="right" @click="$goURl('/pages/login/forgets')">
						忘记密码
					</view>
				</view>
				<view class="bottom" >
					<label class="radio" @click="checkedchange"
						style="display: flex;align-items: center;justify-content: center;">
						<uni-icons type="checkbox" color="#00C356" v-if="checked" size="20"></uni-icons>
						<uni-icons type="circle" v-else size="20"></uni-icons>
						我已阅读并同意<text v-if="agreement!=2"
							@click="$goURl('/subPackages/personal/agreement/agreement?code=1')">《用户协议》</text><text
							style="color: #666;" v-if="agreement==3">与</text><text v-if="agreement!=1"
							@click="$goURl('/subPackages/personal/agreement/agreement?code=0')">《隐私政策》</text>
					</label>
				</view>
				<view class="bottom" @click="$goURl('/pages/login/register')">
					没有账号?<text>去注册</text>
				</view>
				<!-- #ifdef H5 -->
				<view class="bottoms" @click="goDownload">
					app下载
				</view>
				<!-- #endif -->
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		getCurrentInstance
	} from "vue";
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";

	const {
		appContext
	} = getCurrentInstance();
	const {
		proxy
	} = getCurrentInstance();

	let showPasswords = ref(true)
	let form = reactive({
		mobile: '',
		password: ''
	})
	let checked = ref(false)
	let showPassword = ref(true)
	let agreement = ref(0)
	onShow(() => {
		agreement.value = uni.getStorageSync('agreement')
	})

	function checkedchange() {
		checked.value = !checked.value
	}

	function login() {
		if (!checked.value) {
			uni.showToast({
				icon: 'none',
				position: 'bottom',
				title: '请同意用户协议与隐私协议'
			});
			return false;
		}
		// if (!(/^1[3456789]\d{9}$/).test(form.mobile)) {
		// 	uni.showToast({
		// 		icon: 'none',
		// 		position: 'bottom',
		// 		title: '手机号不正确'
		// 	});
		// 	return false;
		// }
		if (form.mobile.length < 1 && form.mobile.length > 20) {
			uni.showToast({
				icon: 'none',
				title: "请输入正确账号"
			})
			return false
		}
		if (form.password.length < 6) {
			uni.showToast({
				icon: 'none',
				title: "请输入密码"
			})
			return false
		}
		appContext.config.globalProperties.$request({
			url: `api/login/`,
			method: 'post',
			data: form,
			success(res) {

				if (res.errno == 0) {
					let user = res.data
					user['password'] = form.password
					uni.setStorageSync('userInfo', user)
					let list = uni.getStorageSync('userList') ? uni.getStorageSync('userList') : []
					list = list.filter(item => {
						return item.ID != res.data.ID
					})
					list.unshift(user)
					uni.setStorageSync('userList', list)
					getExplain() //获取首页数据
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/personal/index'
						})
					}, 1000)
				} else {
					uni.showToast({
						icon: 'none',
						title: res.errmsg
					})
				}
			}
		})
	}

	function getExplain() {
		appContext.config.globalProperties.$request({
			url: `api/decoration/`,
			method: 'get',
			success(res) {
				if (res.errno == 0) {
          const  explainData=res.data.explain||[]
          uni.setStorageSync('explain', explainData)
				} else {
					uni.showToast({
						icon: 'none',
						title: res.errmsg
					})
				}
			}
		})
	}

	function changePassword() {
		showPassword.value = !showPassword.value;
	}

	function changePasswords() {
		console.log('showPassword', showPassword.value)
		showPasswords.value = !showPasswords.value;
	}

	function goDownload() {
		console.log("$download", proxy.$download)
		var u = navigator.userAgent,
			app = navigator.appVersion;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
		var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
		// 是安卓浏览器
		if (isAndroid) {
			window.location.href = proxy.$download; // 跳安卓端下载地址
		}
		// 是iOS浏览器
		if (isIOS) {
			window.location.href = proxy.$download; // 跳AppStore下载地址
		}

		// 是微信内部webView
		if (this.is_weixn()) {
			alert("请点击右上角按钮, 点击使用浏览器打开,注意苹果用户请使用苹果自带的Safari 浏览器打开");
		}

		// 是PC端
		//if (IsPC()) {
		//   window.location.href = 'http://192.168.0.101:8000/'; // 公司主页
		//}
	}

	function is_weixn() {
		var ua = navigator.userAgent.toLowerCase();
		if (ua.match(/MicroMessenger/i) == "micromessenger") {
			return true;
		} else {
			return false;
		}
	}

	function IsPC() {
		var userAgentInfo = navigator.userAgent;
		var Agents = ["Android", "iPhone",
			"SymbianOS", "Windows Phone",
			"iPad", "iPod"
		];
		var flag = true;
		for (var v = 0; v < Agents.length; v++) {
			if (userAgentInfo.indexOf(Agents[v]) > 0) {
				flag = false;
				break;
			}
		}
		return flag;
	}
</script>
<style lang="scss" scoped>
	@import '@/static/css/login.scss';
</style>
